#include "wxsmenu.h"
#include "wxsmenuitem.h"
#include "wxsmenueditor.h"
#include "../wxsitemresdata.h"
#include <globals.h>
#include <prep.h>
#include "scrollingdialog.h"

namespace {
  wxsRegisterItem<wxsMenu> Reg( _T( "Menu" ), _T( "动画控制" ), wxsTTool, _T( "工具" ), 70, false );
  class MenuEditorDialog: public wxScrollingDialog {
    public:
      wxsMenuEditor* Editor;
      MenuEditorDialog( wxsMenu* Menu ):
        wxScrollingDialog( 0, -1, _T( "Menu editor" ), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER ) {
        wxBoxSizer* Sizer = new wxBoxSizer( wxVERTICAL );
        Sizer->Add( Editor = new wxsMenuEditor( this, Menu ), 1, wxEXPAND, 0 );
        Sizer->Add( CreateButtonSizer( wxOK | wxCANCEL ), 0, wxEXPAND, 15 );
        SetSizer( Sizer );
        Sizer->SetSizeHints( this );
        PlaceWindow( this, pdlCentre, true );
      }
      void OnOK( M_Code_unused wxCommandEvent& event ) {
        Editor->ApplyChanges();
        EndModal( wxID_OK );
      }
      DECLARE_EVENT_TABLE()
  };
  BEGIN_EVENT_TABLE( MenuEditorDialog, wxScrollingDialog )
    EVT_BUTTON( wxID_OK, MenuEditorDialog::OnOK )
  END_EVENT_TABLE()
}

wxsMenu::wxsMenu( wxsItemResData* Data ):
  wxsTool( Data, &Reg.Info, 0, 0, flVariable | flSubclass | flExtraCode ) {
}

void wxsMenu::OnBuildCreatingCode() {
  switch( GetLanguage() ) {
  case wxsCPP:
    AddHeader( _T( "<wx/menu.h>" ), GetInfo().ClassName, hfInPCH );
    if( IsPointer() ) {
      Codef( _T( "%C();\n" ) );
    }
    for( int i = 0; i < GetChildCount(); i++ ) {
      GetChild( i )->BuildCode( GetCoderContext() );
    }
    if( GetParent() && GetParent()->GetClassName() == _T( "wxMenuBar" ) ) {
      Codef( _T( "%MAppend(%O, %t);\n" ), m_Label.wx_str() );
    }
    BuildSetupWindowCode();
    break;
  case wxsUnknownLanguage:
  default:
    wxsCodeMarks::Unknown( _T( "wxsMenu::OnBuildCreatingCode" ), GetLanguage() );
  }
}

void wxsMenu::OnEnumToolProperties( M_Code_unused long Flags ) {
  if( GetParent() ) {
    WXS_SHORT_STRING( wxsMenu, m_Label, _T( "Title" ), _T( "label" ), _T( "" ), true );
  }
}

bool wxsMenu::OnMouseDClick( M_Code_unused wxWindow* Preview, M_Code_unused int PosX, M_Code_unused int PosY ) {
  MenuEditorDialog Dlg( this );
  Dlg.ShowModal();
  return false;
}

bool wxsMenu::OnIsPointer() {
  if( !GetParent() ) {
    return false;
  }
  return true;
}

bool wxsMenu::OnCanAddChild( wxsItem* Item, bool ShowMessage ) {
  if( Item->GetInfo().ClassName != _T( "wxMenuItem" ) ) {
    if( ShowMessage ) {
      msging( _T( "Only wxMenuItem classes can be added into wxMenu" ) );
    }
    return false;
  }
  return true;
}

bool wxsMenu::OnCanAddToParent( wxsParent* Parent, bool ShowMessage ) {
  if( Parent->GetClassName() != _T( "wxMenuBar" ) ) {
    if( ShowMessage ) {
      msging( _T( "wxMenu can be added into wxMenuBar only" ) );
    }
    return false;
  }
  return true;
}

bool wxsMenu::OnXmlReadChild( TiXmlElement* Elem, bool IsXRC, bool IsExtra ) {
  if( IsXRC ) {
    wxString ClassName = cbC2U( Elem->Attribute( "class" ) );
    if( ClassName == _T( "separator" ) || ClassName == _T( "break" ) ) {
      wxsMenuItem* Child = new wxsMenuItem( GetResourceData(), true );
      AddChild( Child );
      return Child->XmlRead( Elem, IsXRC, IsExtra );
    }
    if( ClassName == _T( "wxMenu" ) || ClassName == _T( "wxMenuItem" ) ) {
      wxsMenuItem* Child = new wxsMenuItem( GetResourceData(), false );
      AddChild( Child );
      return Child->XmlRead( Elem, IsXRC, IsExtra );
    }
  }
  return true;
}

wxString wxsMenu::OnGetTreeLabel( int& Image ) {
  if( GetParent() ) {
    return m_Label;
  }
  return wxsItem::OnGetTreeLabel( Image );
}
